﻿using Coldairarrow.Entity.Base;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.IBusiness.DTO;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using Quartz.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_SupplierBusiness
    {
        public async Task<PageResult<PB_Supplier>> QueryDataListAsync(PageInput<PBSupplierCoditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_Supplier>();
            var search = input.Search;

            if (!search.Type.IsNullOrWhiteSpace())
            {
                where = where.And(p => p.Type == search.Type);
            }

            if (!search.KeyWord.IsNullOrWhiteSpace())
            {
                where = where.And(p => p.Code.Contains(search.KeyWord) || p.Name.Contains(search.KeyWord));
            }

            where = where.And(x=>x.States==true);
            return await q.Where(where).GetPageResultAsync(input);
        }
        public async Task<PageResult<PB_SupplierDTO>> QueryDataListPB_Supplier(PageInput<PBSupplierCoditionDTO> input)
        {
         
            var where = LinqHelper.True<PB_SupplierDTO>();
            var search = input.Search;

            Expression<Func <PB_Supplier, Base_EnumItem, PB_SupplierDTO>> select = (a, b) => new PB_SupplierDTO
            {
                TypeNmae=b.Name,
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<Base_EnumItem>() on a.Type equals b.Value into ab
                    from b in ab.Where(u => u.EnumCode == "SupplierType").DefaultIfEmpty()
                    select @select.Invoke(a, b);
            if (!search.Condition.IsNullOrEmpty() && !search.KeyWord.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_SupplierDTO, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.KeyWord);
                where = where.And(newWhere);
            }
            return await q.Where(where).GetPageResultAsync(input);


        }
        public async Task<List<PB_Supplier>> QueryAllDataAsync()
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_Supplier>();

            return await q.Where(where).ToListAsync();
        }

        public async Task<List<PB_Supplier>> GetQueryData(SelectQueryDTO search)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_Supplier>();
            if (!search.Keyword.IsNullOrEmpty())
                where = where.And(w => w.Name.Contains(search.Keyword) || w.Code.Contains(search.Keyword));
            var result = await q.Where(where).OrderBy(o => o.Name).Take(search.Take).ToListAsync();

            if (!search.Id.IsNullOrEmpty())
            {
                var one = await this.GetIQueryable().Where(w => w.Id == search.Id).SingleOrDefaultAsync();
                result.Add(one);
            }
            return result;
        }
    }
}